<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户管理
 * @exception 400 参数错误
 * @exception 502 服务器错误
 * @exception 503 数据库错误
 */
class AccountController extends CI_Controller
{
    /**
     * 用户获取用户列表
     */
    function userlist()
    {
        $page = (int)$this->input->get('pageIndex',false,1);
        $count = (int)$this->input->get('pageSize',false,10);
        $keyword = $this->input->get('keyword');
        if ($keyword){
            $this->db->group_start()
            ->like('user.name', $keyword)
            ->or_like('user.nickName', $keyword)
            ->group_end();
        }
        $this->db->join('userRole', 'user.id = userRole.uid', 'left')
        ->join('role', 'userRole.rid = role.id', 'left');
        $total = $this->db->count_all_results('user', FALSE);
        $data = $this->db->select('user.id, user.name ,user.nickName,GROUP_CONCAT(role.name, "") AS role,user.time,user.status')
        ->order_by('user.id','desc')
        ->group_by('user.id')
        ->limit($count,($page-1)*$count)->get()->result_array();

        restful(200, ['total' => $total, 'data' => $data]);
    }


    /**
     * 用户获取用户详情
     * @return int id 用户id
     * @return string username 用户名
     * @return string name 用户姓名
     * @return int sex 性别 1男 2女
     * @return int phone 联系电话
     * @return array department 部门
         * @return int deparment.id 部门id
         * @return string department.name 部门名称
     * @return array role 用户组
         * @return int role.id 用户组id
         * @return string role.name 用户组名称
     */
    function Detail()
    {
        $data = (int)$this->input->get('id');
        $user = $this->db->select('id, name AS username, nickName AS name')->where('id', $data)->get('user')->row_array();
        // 部门
        $user['department'] = $this->db->select('id, name')
            ->join('userDept', 'userDept.did = department.id')
            ->where('userDept.uid', $data)
            ->get('department')->result_array();
        // 用户组
        $user['role'] = $this->db->select('id, name')
            ->join('userRole', 'userRole.rid = role.id')
            ->where('userRole.uid', $data)
            ->get('role')->result_array();

        restful(200, $user);
    }
    /**
     * 用户添加
     * @throws MyException
     */
    function addUser()
    {
        $data = $this->input->post(['name','nickName','role','department','password']);
        if(!$data) throw new MyException('', MyException::INPUT_MISS);
        $result = $this->account->add($data);
        if ($result !== false) {
            restful(201, '操作成功');
        }
        throw new MyException('', MyException::DATABASE);
    }
    /**
     * 用户修改
     * @throws MyException
     */
    function modUser()
    {
        $data = $this->input->put(['id','nickName','role','department']);
        if(!$data) throw new MyException('', MyException::INPUT_MISS);
        $this->account->updateBasic($data['id'], $data['nickName']);
        $this->account->updateRole($data['id'], json_decode($data['role'],true)?:[]);
        $this->account->updateDept($data['id'], json_decode($data['department'],true)?:[]);
        restful();
    }
    /**
     * 重置用户密码
     * @throws MyException
     */
    public function modPwd(int $id=0)
    {
        $pwd = $this->input->put('pwd');
        $result = $this->account->resetPwd($id, $pwd);
        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }
    /**
     * 用户 启用/禁用
     * @throws MyException
     */
    public function modStatus()
    {
        $data = $this->input->put(['id','status']);
        if(!$data) throw new MyException('', MyException::INPUT_MISS);
        $result = $this->account->updateStatus($data['id'], $data['status']);
        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }

}
